package controllers;

import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JTable;

import models.CustomTable;
import models.CustomTableModel;
import models.DailySalesModel;
import system.View;
import dialogs.DailySalesDialog;

public class DailySalesController extends AbstractController {

	private DailySalesModel salesModel = new DailySalesModel();

	public DailySalesController(View v) {
		super(v);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String actionCommand = e.getActionCommand();
		if (actionCommand.equals("Daily Sales Report"))
		{
			showDialogWindow(new DailySalesDialog(view, this));
		}
	}

	public int generateDailySales(String store_name)
	{
		boolean flag= false;
		if(store_name.length()==0)
			return VALIDATIONERROR;
		if(salesModel.checkForDailySales(store_name))
		{
			salesModel.deleteDailySales(store_name);
			if(salesModel.getDailySales(store_name))
				flag = true;
		}
		else if(salesModel.getDailySales(store_name))
			flag = true;
		ResultSet rs =salesModel.printDailySales(store_name);
		ResultSet rs2 = salesModel.printDailySales(store_name);
		ResultSet rs3 = salesModel.printDailySales(store_name);
		if(flag==true && rs!=null)
		{
			CustomTableModel model = new CustomTableModel(salesModel.getConnection(), rs);
			CustomTable data = new CustomTable(model);
			model.addExceptionListener(this);
			data.addExceptionListener(this);
			    
			view.addTable(data);
			
			int[] total = salesModel.getTotalValuePerCategory(rs2);
			String[][] total_data = {
					{"ROCK", Integer.toString(total[0])},
					{"POP", Integer.toString(total[1])},
					{"RAP", Integer.toString(total[2])},
					{"COUNTRY", Integer.toString(total[3])},
					{"CLASSICAL", Integer.toString(total[4])},
					{"NEW AGE", Integer.toString(total[5])},
					{"INSTRUMENTAL", Integer.toString(total[6])},
					{"All Categories", Integer.toString(salesModel.getTotalValue(rs3))}
			};
			JTable table = new JTable(total_data, new String[]{ "Category", "Total Sales"});
			
			view.addAuxiliaryTable(table);

			return operationSuccess();
		}
		return operationFailure();
	}

	@Override
	public int validateInsert(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public int validateDelete(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}

}
